﻿static char *plugin_id = "@(#)Copyright (C) gaoguangtao   plugin.cpp	Ver2.06";

//Dll plugin加载源文件
/* ========================================================================
	Project  Name			: IP Messenger for Win32
	Module Name				: Plug-in
	Create					: 2008-02-20(Sat)
    Update					: 2008-02-14(Wed)
	Copyright				: gaoguangtao
	Reference				: 
	======================================================================== */

#include "tlib.h"
#include "resource.h"
#include "ipmsg.h"
#include "msgstr.h"

class Plugin : public TListObj {
public:
	HINSTANCE	*plug_dll;
	int			plug_cnt;

	Plugin(char *dllname);
	HINSTANCE EntryDll(char *dllName);
	BOOL ExitDll(HINSTANCE dll);

public:
	Plugin(void);
	~Plugin();
};

class PluginMng {
protected:
	TList plugin;

public:
	PluginMng(void);
	~PluginMng();
};

Plugin::Plugin(void)
{
}

Plugin::~Plugin(void)
{
	while (plug_cnt > 0)
		ExitDll(plug_dll[plug_cnt -1]);

	if (plug_dll)
		free(plug_dll);
}

HINSTANCE Plugin::EntryDll(char *dllName)
{
	HINSTANCE dll;
	BOOL (*pPluginInit)(void);

	dll = ::LoadLibrary(dllName);
	if (dll == NULL)
		return	NULL;

	pPluginInit = (BOOL (*)(void))::GetProcAddress(dll, "PluginInitialize");
	if (pPluginInit != NULL && pPluginInit())
	{
		plug_dll = (HINSTANCE *)realloc(plug_dll, sizeof(HINSTANCE) * (plug_cnt + 1));
		if (plug_dll != NULL)
		{
			plug_dll[plug_cnt++] = dll;
			
			return dll;
		}
	}

	::FreeLibrary(dll);
	
	return NULL;
}

BOOL Plugin::ExitDll(HINSTANCE dll)
{
	BOOL (*pPlugTerm)(void);

	for (int cnt = 0; cnt < plug_cnt; cnt++)
	{
		if (plug_dll[cnt] != dll)
			continue;

		pPlugTerm = (BOOL (*)(void))::GetProcAddress(plug_dll[cnt], "PluginTermiante");
		if (pPlugTerm != NULL)
			pPlugTerm();

		::FreeLibrary(plug_dll[cnt]);
		memmove(plug_dll + cnt, plug_dll + cnt +1, (--plug_cnt - cnt) * sizeof(HINSTANCE));
		
		return TRUE;
	}

	return	FALSE;
}